{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 简介\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>YiJingFramework.PrimitiveTypes.GanzhiCombinations, 2.0.1</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget:YiJingFramework.PrimitiveTypes.GanzhiCombinations\"\n",
    "using YiJingFramework.PrimitiveTypes;\n",
    "using YiJingFramework.PrimitiveTypes.GanzhiCombinations;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此包提供了一些类型，以表示干支的组合。目前包括 `Ganzhi` 和 `TianganOrDizhi` 两个类型。\n",
    "\n",
    "## 干支\n",
    "\n",
    "此包中的 `Ganzhi` 可以表示六十甲子中的一对干支。在 [YiJingFramework.Nongli](https://yjfwk.yueyinqiu.top/Nongli/) 中，就使用了此类型以表示年月日时。\n",
    "\n",
    "此类型要求干支的阴阳是相互匹配的，不存在“甲丑”之类的情况，并且内部只使用一个数字来记录它在六十甲子中的情况，而非分别记录天干和地支。这边给一个相对简单的用例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jiazi 的下一个是 乙丑\n",
      "3\n",
      "丙寅 -> 丙 寅\n"
     ]
    }
   ],
   "source": [
    "var jiazi = Ganzhi.FromGanzhi(Tiangan.Jia, Dizhi.Zi);\n",
    "var yichou = jiazi.Next(1);\n",
    "Console.WriteLine($\"{jiazi} 的下一个是 {yichou:C}\");\n",
    "\n",
    "var bingyin = Ganzhi.FromIndex(yichou.Index + 1);\n",
    "Console.WriteLine(bingyin.Index);\n",
    "\n",
    "var (bing, _) = bingyin;\n",
    "var yin = bingyin.Dizhi;\n",
    "Console.WriteLine($\"{bingyin:C} -> {bing:C} {yin:C}\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 天干或地支\n",
    "\n",
    "此包中的 `TianganOrDizhi` 表示一个天干或一个地支。在某些不需要或者无法区分天干地支的情况下可能比较方便：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "YouWuJiSiMaoWuSiZiHaiMao\n",
      "\n",
      "1\n",
      "False\n",
      "False\n",
      "Wu\n",
      "\n",
      "2\n",
      "False\n",
      "True\n",
      "Ji\n",
      "False\n",
      "\n",
      "0\n",
      "You\n",
      "System.InvalidCastException\n",
      "\n"
     ]
    }
   ],
   "source": [
    "using YiJingFramework.PrimitiveTypes;\n",
    "using YiJingFramework.PrimitiveTypes.GanzhiCombinations;\n",
    "\n",
    "Random random = new Random(0);\n",
    "\n",
    "IEnumerable<TianganOrDizhi> EnumerateEntities()\n",
    "{\n",
    "    for(; ; )\n",
    "    {\n",
    "        var isTiangan = random.Next(0, 2) is 0;\n",
    "        if (isTiangan)\n",
    "            yield return Tiangan.FromIndex(random.Next(1, 10 + 1));\n",
    "        else\n",
    "            yield return Dizhi.FromIndex(random.Next(1, 12 + 1));\n",
    "    }\n",
    "}\n",
    "\n",
    "var entites = EnumerateEntities().Take(10).ToArray();\n",
    "Console.WriteLine(string.Concat(entites));\n",
    "Console.WriteLine();\n",
    "\n",
    "Console.WriteLine(\"1\");\n",
    "Console.WriteLine(entites[1].IsTiangan);\n",
    "Console.WriteLine(entites[1].TryAsTiangan(out var tiangan1, out var dizhi1));\n",
    "Console.WriteLine(dizhi1);\n",
    "Console.WriteLine();\n",
    "\n",
    "Console.WriteLine(\"2\");\n",
    "Console.WriteLine(entites[2].IsDizhi);\n",
    "Console.WriteLine(entites[2].TryAsTiangan(out var tiangan2));\n",
    "Console.WriteLine(tiangan2);\n",
    "Console.WriteLine(entites[2].TryAsDizhi(out var dizhi2));\n",
    "Console.WriteLine();\n",
    "\n",
    "Console.WriteLine(\"0\");\n",
    "Console.WriteLine((Dizhi)entites[0]);\n",
    "try\n",
    "{\n",
    "    Console.WriteLine((Tiangan)entites[0]);\n",
    "}\n",
    "catch(Exception e)\n",
    "{\n",
    "    Console.WriteLine(e.GetType());\n",
    "}\n",
    "Console.WriteLine();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "python",
   "pygments_lexer": "csharp",
   "version": "3.12.2"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
